package com.ts.api.http.platform.biz;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.algorithm.encrypt.symmetric.AES;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.base.MapUtils;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.util.id.IdUtils;
import com.gitee.apanlh.util.reflection.ClassConvertUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.gitee.apanlh.web.model.vo.RequestVO;
import com.ts.api.common.util.H5DomainUrlUtils;
import com.ts.api.http.platform.entity.apply.ApiPlatApplyYouRenRong;
import com.ts.api.http.platform.entity.dataexchange.ApiPlatDataExchangeYouRenRong;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiBizStatus;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.platform.entity.RespPlatformNoticeApply;
import com.ts.api.module.api.convert.platform.entity.RespPlatformPhoneMd5Apply;
import com.ts.api.module.api.convert.platform.entity.RespPlatformPhoneMd5DataExchange;
import com.ts.api.module.api.convert.platform.handler.ApiPlatformPhoneMd5ConvertHandler;

import java.math.BigDecimal;
import java.util.Map;

/**
 * 优任融
 *
 * 半流程-撞库返回授权地址：https://doc.apipost.net/docs/detail/38777022b807000?target_id=4b15c32371006&locale=zh-cn
 *
 * @author WJB
 */
@ApiPlan(productId = "10", type = ApiPlanConvertType.PLAT_PHONE_MD5)
public class ApiPlatYouRenRongPhoneMd5Convert implements ApiPlatformPhoneMd5ConvertHandler {

    public static final String KEY = "cu9vys51f8GEWxn4";
    public static final String CHANNEL_ID = "6e8EFCLA";

    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) {
        ApiPlatDataExchangeYouRenRong data = new ApiPlatDataExchangeYouRenRong();
        data.setPhoneMd5(apiDataExchangeParameter.getPhoneMd5());
        data.setUserName("浩安星");
        data.setAge(ClassConvertUtils.toInt(apiDataExchangeParameter.getAge()));
        //  性别 0-女 1-男 默认1
        String sex = apiDataExchangeParameter.getSex();
            Integer sexChoose = ChooseEq.create()
                    .when(sex, "0", 1)
                    .when(sex, "1", 0)
                    .end(1);
        data.setSex(sexChoose);
        //  城市编码
        data.setAreaCode(apiDataExchangeParameter.getAreaCode());
        //  城市
        data.setCity(apiDataExchangeParameter.getCity());
        //  芝麻分
        Integer zhima = ChooseEq.create()
                .when(apiDataExchangeParameter.getSesameCredit(), "1", 2)
                .when(apiDataExchangeParameter.getSesameCredit(), "2", 3)
                .when(apiDataExchangeParameter.getSesameCredit(), "3", 4)
                .when(apiDataExchangeParameter.getSesameCredit(), "4", 5)
                .end(2);
        data.setSesameCredit(zhima);
        //  公积金
        Integer gongjijin = ChooseEq.create()
                .when(apiDataExchangeParameter.getProvidentFund(), "1", 1)
                .when(apiDataExchangeParameter.getProvidentFund(), "2", 2)
                .when(apiDataExchangeParameter.getProvidentFund(), "3", 2)
                .when(apiDataExchangeParameter.getProvidentFund(), "4", 2)
                .end(1);
        data.setProvidentFund(gongjijin);
        //  社保
        Integer shebao = ChooseEq.create()
                .when(apiDataExchangeParameter.getSocialSecurity(), "1", 1)
                .when(apiDataExchangeParameter.getSocialSecurity(), "2", 2)
                .when(apiDataExchangeParameter.getSocialSecurity(), "3", 2)
                .when(apiDataExchangeParameter.getSocialSecurity(), "4", 2)
                .end(1);
        data.setSocialSecurity(shebao);
        //  保单
        Integer baodanIs = ChooseEq.create()
                .when(apiDataExchangeParameter.getInsurancePolicy(), "1", 1)
                .when(apiDataExchangeParameter.getInsurancePolicy(), "2", 2)
                .when(apiDataExchangeParameter.getInsurancePolicy(), "3", 2)
                .when(apiDataExchangeParameter.getInsurancePolicy(), "4", 2)
                .end(1);
        data.setInsurancePolicy(baodanIs);
        //  房产情况
        Integer house = ChooseEq.create()
                .when(apiDataExchangeParameter.getRealEstateInfo(), "1", 1)
                .when(apiDataExchangeParameter.getRealEstateInfo(), "2", 2)
                .when(apiDataExchangeParameter.getRealEstateInfo(), "3", 2)
                .when(apiDataExchangeParameter.getRealEstateInfo(), "4", 2)
                .when(apiDataExchangeParameter.getRealEstateInfo(), "5", 2)
                .when(apiDataExchangeParameter.getRealEstateInfo(), "6", 2)
                .end(1);
        data.setRealEstateInfo(house);
        //  车产
        Integer car = ChooseEq.create()
                .when(apiDataExchangeParameter.getCarInfo(), "1", 1)
                .when(apiDataExchangeParameter.getCarInfo(), "2", 2)
                .when(apiDataExchangeParameter.getCarInfo(), "3", 2)
                .when(apiDataExchangeParameter.getCarInfo(), "4", 2)
                .end(1);
        data.setCarInfo(car);
        //  职业
        Integer job = ChooseEq.create()
                .when(apiDataExchangeParameter.getCareer(), "1", 1)
                .when(apiDataExchangeParameter.getCareer(), "2", 5)
                .when(apiDataExchangeParameter.getCareer(), "3", 3)
                .when(apiDataExchangeParameter.getCareer(), "4", 2)
                .when(apiDataExchangeParameter.getCareer(), "5", 4)
                .end(1);
        data.setCareer(job);
        //  营业执照
        Integer bizLicenseYear = ChooseEq.create()
                .when(apiDataExchangeParameter.getBizLicenseYear(), "1", 1)
                .when(apiDataExchangeParameter.getBizLicenseYear(), "2", 2)
                .when(apiDataExchangeParameter.getBizLicenseYear(), "3", 3)
                .when(apiDataExchangeParameter.getBizLicenseYear(), "4", 4)
                .end(1);
        data.setBizLicenseYear(bizLicenseYear);
        //  贷款金额
        Integer money = ChooseEq.create()
                .when(apiDataExchangeParameter.getLoanAmount(), "1", 30000)
                .when(apiDataExchangeParameter.getLoanAmount(), "2", 50000)
                .when(apiDataExchangeParameter.getLoanAmount(), "3", 100000)
                .when(apiDataExchangeParameter.getLoanAmount(), "4", 200000)
                .end(30000);
        data.setLoanAmount(money);
        //  ip
        if (ValidParam.isNotEmpty(apiDataExchangeParameter.getIp())) {
            data.setIp(apiDataExchangeParameter.getIp());
        } else {
            data.setIp("127.0.0.1");
        }
        //  客户端类型
        data.setPlatform(1);
        //  月收入
        Integer monthlySalary = ChooseEq.create()
                .when(apiDataExchangeParameter.getMonthlySalary(), "1", 1)
                .when(apiDataExchangeParameter.getMonthlySalary(), "2", 2)
                .when(apiDataExchangeParameter.getMonthlySalary(), "3", 3)
                .when(apiDataExchangeParameter.getMonthlySalary(), "4", 4)
                .when(apiDataExchangeParameter.getMonthlySalary(), "5", 6)
                .end(1);
        data.setMonthlySalary(monthlySalary);
        AES aes = AES.createEcb(KEY.getBytes());
        String dataStr = aes.encryptToBase64Str(JsonUtils.toJson(data));

        Map<String, Object> map = MapUtils.newHashMap(newMap -> {
            newMap.put("channelCode", CHANNEL_ID);
            newMap.put("content", dataStr);
            newMap.put("serialNo", IdUtils.getSimpleUuid());
        });
        return JsonUtils.toJson(map);
    }


    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) {
        ApiPlatApplyYouRenRong data = new ApiPlatApplyYouRenRong();
        data.setApplyNo(apiApplyParameter.dataExchangeApplyId());
        data.setPhone(apiApplyParameter.getPhone());
        data.setUserName(apiApplyParameter.getUserName());
        data.setAge(ClassConvertUtils.toInt(apiApplyParameter.getAge()));
        //  性别 0-女 1-男 默认1
        String sex = apiApplyParameter.getSex();
        Integer sexChoose = ChooseEq.create()
                .when(sex, "0", 1)
                .when(sex, "1", 0)
                .end(1);
        data.setSex(sexChoose);
        //  城市编码
        data.setAreaCode(apiApplyParameter.getAreaCode());
        //  城市
        data.setCity(apiApplyParameter.getCity());
        //  芝麻分
        Integer zhima = ChooseEq.create()
                .when(apiApplyParameter.getSesameCredit(), "1", 2)
                .when(apiApplyParameter.getSesameCredit(), "2", 3)
                .when(apiApplyParameter.getSesameCredit(), "3", 4)
                .when(apiApplyParameter.getSesameCredit(), "4", 5)
                .end(2);
        data.setSesameCredit(zhima);
        //  公积金
        Integer gongjijin = ChooseEq.create()
                .when(apiApplyParameter.getProvidentFund(), "1", 1)
                .when(apiApplyParameter.getProvidentFund(), "2", 2)
                .when(apiApplyParameter.getProvidentFund(), "3", 2)
                .when(apiApplyParameter.getProvidentFund(), "4", 2)
                .end(1);
        data.setProvidentFund(gongjijin);
        //  社保
        Integer shebao = ChooseEq.create()
                .when(apiApplyParameter.getSocialSecurity(), "1", 1)
                .when(apiApplyParameter.getSocialSecurity(), "2", 2)
                .when(apiApplyParameter.getSocialSecurity(), "3", 2)
                .when(apiApplyParameter.getSocialSecurity(), "4", 2)
                .end(1);
        data.setSocialSecurity(shebao);
        //  保单
        Integer baodanIs = ChooseEq.create()
                .when(apiApplyParameter.getInsurancePolicy(), "1", 1)
                .when(apiApplyParameter.getInsurancePolicy(), "2", 2)
                .when(apiApplyParameter.getInsurancePolicy(), "3", 2)
                .when(apiApplyParameter.getInsurancePolicy(), "4", 2)
                .end(1);
        data.setInsurancePolicy(baodanIs);
        //  房产情况
        Integer house = ChooseEq.create()
                .when(apiApplyParameter.getRealEstateInfo(), "1", 1)
                .when(apiApplyParameter.getRealEstateInfo(), "2", 2)
                .when(apiApplyParameter.getRealEstateInfo(), "3", 2)
                .when(apiApplyParameter.getRealEstateInfo(), "4", 2)
                .when(apiApplyParameter.getRealEstateInfo(), "5", 2)
                .when(apiApplyParameter.getRealEstateInfo(), "6", 2)
                .end(1);
        data.setRealEstateInfo(house);
        //  车产
        Integer car = ChooseEq.create()
                .when(apiApplyParameter.getCarInfo(), "1", 1)
                .when(apiApplyParameter.getCarInfo(), "2", 2)
                .when(apiApplyParameter.getCarInfo(), "3", 2)
                .when(apiApplyParameter.getCarInfo(), "4", 2)
                .end(1);
        data.setCarInfo(car);
        //  职业
        Integer job = ChooseEq.create()
                .when(apiApplyParameter.getCareer(), "1", 1)
                .when(apiApplyParameter.getCareer(), "2", 5)
                .when(apiApplyParameter.getCareer(), "3", 3)
                .when(apiApplyParameter.getCareer(), "4", 2)
                .when(apiApplyParameter.getCareer(), "5", 4)
                .end(1);
        data.setCareer(job);
        //  营业执照
        Integer bizLicenseYear = ChooseEq.create()
                .when(apiApplyParameter.getBizLicenseYear(), "1", 1)
                .when(apiApplyParameter.getBizLicenseYear(), "2", 2)
                .when(apiApplyParameter.getBizLicenseYear(), "3", 3)
                .when(apiApplyParameter.getBizLicenseYear(), "4", 4)
                .end(1);
        data.setBizLicenseYear(bizLicenseYear);
        //  贷款金额
        Integer money = ChooseEq.create()
                .when(apiApplyParameter.getLoanAmount(), "1", 30000)
                .when(apiApplyParameter.getLoanAmount(), "2", 50000)
                .when(apiApplyParameter.getLoanAmount(), "3", 100000)
                .when(apiApplyParameter.getLoanAmount(), "4", 200000)
                .end(30000);
        data.setLoanAmount(money);
        //  ip
        if (ValidParam.isNotEmpty(apiApplyParameter.getIp())) {
            data.setIp(apiApplyParameter.getIp());
        } else {
            data.setIp("127.0.0.1");
        }
        //  客户端类型
        data.setPlatform(1);
        //  月收入
        Integer monthlySalary = ChooseEq.create()
                .when(apiApplyParameter.getMonthlySalary(), "1", 1)
                .when(apiApplyParameter.getMonthlySalary(), "2", 2)
                .when(apiApplyParameter.getMonthlySalary(), "3", 3)
                .when(apiApplyParameter.getMonthlySalary(), "4", 4)
                .when(apiApplyParameter.getMonthlySalary(), "5", 6)
                .end(1);
        data.setMonthlySalary(monthlySalary);
        //  协议
        data.setProtocolUrl(H5DomainUrlUtils.createUserProtocolUrl(apiApplyParameter.getProductPlanId(), apiApplyParameter.getPhoneMd5()));

        AES aes = AES.createEcb(KEY.getBytes());
        String dataStr = aes.encryptToBase64Str(JsonUtils.toJson(data));

        Map<String, Object> map = MapUtils.newHashMap(newMap -> {
            newMap.put("channelCode", CHANNEL_ID);
            newMap.put("content", dataStr);
            newMap.put("serialNo", IdUtils.getSimpleUuid());
        });
        return map;
    }

    @Override
    public RespPlatformPhoneMd5Apply respApply(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespPlatformPhoneMd5Apply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 200)) {
            return RespPlatformPhoneMd5Apply.ofFail();
        }
        return RespPlatformPhoneMd5Apply.ofSuc();
    }

    @Override
    public RespPlatformPhoneMd5DataExchange respDataExchange(String respJson) {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespPlatformPhoneMd5DataExchange.ofFail();
        }
        boolean code = Eq.object(jsonObject.getInteger("code"), 200);
        if (!code) {
            return RespPlatformPhoneMd5DataExchange.ofFail();
        }
        JSONObject resData = JsonUtils.get(respJson).getJSONObject("data");

        BigDecimal price = resData.getBigDecimal("price");
        String companyName = resData.getString("companyName");
        String productName = resData.getString("productName");
        String logo = resData.getString("logo");
        String orderId = resData.getString("applyNo");
        String callUrl = resData.getString("url");

        RespPlatformPhoneMd5DataExchange result = RespPlatformPhoneMd5DataExchange.ofSuc();

        result.setPrice(price);
        result.setLogoPath(logo);
        result.setDisplayName(companyName);
        result.setProductName(productName);
        result.setApplyId(orderId);
        result.setCallUrl(callUrl);
        return result;
    }

    @Override
    public RespPlatformNoticeApply parseApplyNotice(RequestVO requestVO) {
        JSONObject jsonObject = JsonUtils.get(requestVO.getBody());
        String phoneMd5 = jsonObject.getString("phoneMd5");

        RespPlatformNoticeApply applyNotice = new RespPlatformNoticeApply();
        applyNotice.setPhoneMd5(phoneMd5);
        return applyNotice;
    }

    @Override
    public Map<String, Object> respApplyNotice(ApiBizStatus apiBizStatus) {
        boolean b = apiBizStatus.hasBizSuc();

        if (b) {
            return MapUtils.newHashMap(newHashMap -> {
                newHashMap.put("code", 200);
                newHashMap.put("msg", "通知进件成功");
            });
        }
        return MapUtils.newHashMap(newHashMap -> {
            newHashMap.put("code", "500");
            newHashMap.put("msg", apiBizStatus.getBizMsg());
        });
    }
}
